
package com.wutianyi.maven.properties;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

/**
 * @author hanjiewu
 *
 */
public class ConfigProperties {
	private static Logger logger = Logger.getLogger(ConfigProperties.class);
	/**
	 * 默认的config.properties文件的路径
	 */
	private static final String	DEFAULT_PROPERTIES_PATH	= System.getProperty("user.home") + File.separator + "config.properties";

	/**
	 * 配置文件对象
	 */
	private Properties			properties;
	/**
	 * 配置文件的路径
	 */
	private String				propertiesPath;

	public ConfigProperties(String propertiesPath) {

		this.propertiesPath = propertiesPath;
		init();
	}

	public ConfigProperties() {

		this(DEFAULT_PROPERTIES_PATH);
	}

	/**
	 * 初始化，如果文件不存在会默认创建一个
	 */
	private void init() {

		File file = new File(propertiesPath);
		FileInputStream inputStream = null;
		try {
			if(!file.exists()) {

				boolean result = file.createNewFile();
				if(result) {
					logger.info("[Create The File]: " + file.getName());
				}
			}
			properties = new Properties();
			inputStream = new FileInputStream(file);

			properties.load(inputStream);
			
		}catch(IOException e) {
			e.printStackTrace();
		} finally {
			try {
				inputStream.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	/**
	 * 关闭文件，将新的配置项的值写入到文件中
	 */
	public void close() {

		File file = new File(propertiesPath);
		FileOutputStream outputStream = null;
		try {
			if(!file.exists()) {
				
				boolean result = file.createNewFile();
				if(result) {
					logger.info("[Create The File]: " + file.getAbsolutePath());
				}
			}
			outputStream = new FileOutputStream(file);
			if(null != properties) {
				properties.store(outputStream, "");
			}
			
		}catch(IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if(null != outputStream) {
					
					outputStream.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	/**
	 * 获取属性值，如果配置文件中不存在，则返回默认的defaultvalue
	 * 如果defaultValue為空，則提示用戶输入
	 * @param key
	 * @param defaultValue
	 * @return
	 */
	public String getProperties(String key, String defaultValue, String description) {
		
		boolean storeProperty = false;
		
		String value = properties.getProperty(key);

		if(StringUtils.isBlank(value)) {
			storeProperty = true;
			value = defaultValue;
		}

		if(StringUtils.isBlank(value)) {
			System.out.println("Enter the key ");
			if(StringUtils.isNotBlank(description)) {
				
				System.out.println(key + "(" + description + ") value: ");
			} else {
				System.out.println(key + " value: ");
			}
			BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
			try {
				value = reader.readLine();
				while(StringUtils.isBlank(value)) {
					
					value = reader.readLine();
				}
				
				storeProperty = true;
			}catch(IOException e) {
				e.printStackTrace();
			}
			
		}
		if(storeProperty) {
			
			properties.setProperty(key, value);
		}
		
		return value;
	}

	public String getProperties(String key, String descritpion) {

		return getProperties(key, null, descritpion);
	}
}
